﻿using System;

namespace SimpleFactoryShape
{
    public abstract class Shape
    {
        public double area;
        public double Area
        {
            get { return area; }
            set { area = value; }
        }
        public abstract void RandomCreate();
        public abstract double Area1();
    }
    public class Triangle : Shape
    {
        double a, b, c,p;
        public override void RandomCreate()
        {
            Random rd = new Random();
            do
            {
                a = rd.Next(1,20);
                b = rd.Next(1,20);
                c = rd.Next(1,20);
            } while (!((a + b > c) & (a + c > b) & (b + c > a)));
            Console.WriteLine("三角形三边：" + a + " " + b + " " + c);
        }
        public override double Area1()
        {
            p = (a + b + c)/2;
            Area = Math.Sqrt(p * (p - a) * (p - b) * (p - c));
            Console.WriteLine("随机生成三角形面积" + Area);
            return Area;
        }
    }
    public class Rectangle : Shape
    {
        double l, h;
        public override double Area1()
        {
            Area = l * h;
            Console.WriteLine("随机生成矩形面积" + Area);
            return Area;
        }
        public override void RandomCreate()
        {
            Random rd = new Random();
            l = rd.Next(1, 20);
            h = rd.Next(1, 20);
            Console.WriteLine("矩形长与宽：" + l + " " + h);
        }
    }
    public class Circular : Shape
    {
        double r;
        public override void RandomCreate()
        {
            Random rd = new Random();
            r = rd.Next(1, 20);
            Console.WriteLine("圆形半径：" + r);

        }
        public override double Area1()
        {
            Area = Math.PI * r * r;
            Console.WriteLine("随机生成圆形面积" + Area);
            return Area;
        }
    }
    public static class ShapeFactory
    {
        public static Shape Create(string Shapename)
        {
            Shape product = null;
            switch (Shapename)
            {
                case "Triangle":
                    product = new Triangle();
                    product.RandomCreate();
                    break;
                case "Rectangle":
                    product = new Rectangle();
                    product.RandomCreate();
                    break;
                case "Circular":
                    product = new Circular();
                    product.RandomCreate();
                    break;
                default:
                    break;
            }
            return product;
        }
    }
    public class test
    {
        static void Main(string[] args)
        {
            double sum=0;
            for(int i = 0; i < 10; i++)
            {
                Console.WriteLine("请输入要创建对象名(Triangle,Rectangle,Circular三者中选择):");
                string Shapename = Console.ReadLine();
                Shape s = ShapeFactory.Create(Shapename);
                sum = sum + s.Area1();

            }
            Console.WriteLine("总面积：" + sum);
        }
    }
}
